package android_serialport_api.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;

public class SerialPortFinder {
    private static final String TAG = "SerialPort";
    private Vector<Driver> mDrivers = null;
    private final int VENDOR_ID = 516;
    private final int PRODUCT_ID = 2954;

    public SerialPortFinder() {
    }

    Vector<Driver> getDrivers(boolean b) throws IOException {
        LineNumberReader r;
        String l;
        String drivername;
        String[] w;
        if (this.mDrivers == null) {
            this.mDrivers = new Vector();
            r = new LineNumberReader(new FileReader("/proc/tty/drivers"));

            while ((l = r.readLine()) != null) {
                drivername = l.substring(0, 21).trim();
                w = l.split(" +");
                if (w.length >= 5 && w[w.length - 1].equals("serial")) {
                    this.mDrivers.add(new SerialPortFinder.Driver(drivername, w[w.length - 4]));
                }
            }

            r.close();
        } else if (b) {
            this.mDrivers.clear();
            r = new LineNumberReader(new FileReader("/proc/tty/drivers"));

            while ((l = r.readLine()) != null) {
                drivername = l.substring(0, 21).trim();
                w = l.split(" +");
                if (w.length >= 5 && w[w.length - 1].equals("serial")) {
                    this.mDrivers.add(new SerialPortFinder.Driver(drivername, w[w.length - 4]));
                }
            }

            r.close();
        }

        return this.mDrivers;
    }

    public String[] getAllDevices(boolean b) {
        Vector devices = new Vector();

        try {
            Iterator itdriv = this.getDrivers(b).iterator();

            while (itdriv.hasNext()) {
                SerialPortFinder.Driver driver = (SerialPortFinder.Driver) itdriv.next();
                Iterator itdev = driver.getDevices().iterator();

                while (itdev.hasNext()) {
                    String device = ((File) itdev.next()).getName();
                    String value = String.format("%s (%s)", device, driver.getName());
                    devices.add(value);
                }
            }
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return (String[]) devices.toArray(new String[devices.size()]);
    }

    public String getCharNodeByUSBid() {
        String USB_SERIAL_DIR = "/sys/class/tty/";
        File usbDir = new File("/sys/class/tty/");
        File[] usbFiles = usbDir.listFiles();
        if (null == usbFiles) {
            return null;
        } else {
            try {
                File[] var6 = usbFiles;
                int var7 = usbFiles.length;

                for (int var8 = 0; var8 < var7; ++var8) {
                    File usbFile = var6[var8];
                    String fileName = usbFile.getName();
                    int vid;
                    int pid;
                    if (fileName.startsWith("ttyACM")) {
                        vid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../idVendor"), 16);
                        pid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../idProduct"), 16);
                        if (vid == 516 && pid == 2954) {
                            return "/dev/" + fileName;
                        }
                    } else if (fileName.startsWith("ttyUSB")) {
                        vid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../../idVendor"), 16);
                        pid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../../idProduct"), 16);
                        if (vid == 516 && pid == 2954) {
                            return "/dev/" + fileName;
                        }
                    }
                }

                return null;
            } catch (Exception var11) {
                return null;
            }
        }
    }

    private String readDevNode(String filePath) {
        String content = null;

        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
            content = reader.readLine();
            reader.close();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return content;
    }

    public String[] getUSBDevicesPath() {
        Vector<String> devices = new Vector();
        String USB_SERIAL_DIR = "/sys/class/tty/";
        File usbDir = new File("/sys/class/tty/");
        File[] usbFiles = usbDir.listFiles();
        if (null == usbFiles) {
            return null;
        } else {
            try {
                File[] var7 = usbFiles;
                int var8 = usbFiles.length;

                for (int var9 = 0; var9 < var8; ++var9) {
                    File usbFile = var7[var9];
                    String fileName = usbFile.getName();
                    int vid;
                    int pid;
                    if (fileName.startsWith("ttyACM")) {
                        vid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../idVendor"), 16);
                        pid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../idProduct"), 16);
                        if (vid == 516 && pid == 2954) {
                            devices.add("/dev/" + fileName);
                        }
                    } else if (fileName.startsWith("ttyUSB")) {
                        vid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../../idVendor"), 16);
                        pid = Integer.parseInt(this.readDevNode(usbFile.getAbsolutePath() + "/../../../../idProduct"), 16);
                        if (vid == 516 && pid == 2954) {
                            devices.add("/dev/" + fileName);
                        }
                    }
                }
            } catch (Exception var12) {
            }

            return devices.size() <= 0 ? null : (String[]) devices.toArray(new String[devices.size()]);
        }
    }

    public String[] getAllDevicesPath(boolean b) {
        Vector devices = new Vector();

        try {
            Iterator itdriv = this.getDrivers(b).iterator();

            while (itdriv.hasNext()) {
                SerialPortFinder.Driver driver = (SerialPortFinder.Driver) itdriv.next();
                Iterator itdev = driver.getDevices().iterator();

                while (itdev.hasNext()) {
                    String device = ((File) itdev.next()).getAbsolutePath();
                    devices.add(device);
                }
            }
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return (String[]) devices.toArray(new String[devices.size()]);
    }

    public class Driver {
        private String mDriverName;
        private String mDeviceRoot;
        Vector<File> mDevices = null;

        public Driver(String name, String root) {
            this.mDriverName = name;
            this.mDeviceRoot = root;
        }

        public Vector<File> getDevices() {
            if (this.mDevices == null) {
                this.mDevices = new Vector();
                File dev = new File("/dev");
                File[] files = dev.listFiles();

                for (int i = 0; i < files.length; ++i) {
                    if (files[i].getAbsolutePath().startsWith(this.mDeviceRoot)) {
                        this.mDevices.add(files[i]);
                    }
                }
            }

            return this.mDevices;
        }

        public String getName() {
            return this.mDriverName;
        }
    }
}